<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>proxy</title>
</head>
<body>
<script>
    /**
     * Reflect.get(target, key, receiver)。
     * Reflect.set(target, key, receiver)。
     * receiver参数是绑定到拦截操作中的this对象。默认是target目标对象。否则是Proxy实例对象。
     * */
    const obj1 = {
        count: 1,
        greet: function(message) {
            console.log(`${message}, ${this.name}!`);
            return `${message}, ${this.name}!`;
        },
    }
    const proxy1 = new Proxy(obj1, {
        get(target, key, receiver) {
            return Reflect.get(target, key, receiver)
        },
        set(target, key, value, receiver) {
            Reflect.set(target, key, value, receiver)
        },
        deleteProperty(target, key) {
            if (key === 'count') {
                console.log('不能删除 count 属性');
                return false;
            }
            return Reflect.deleteProperty(target, key)
        }
    })
    proxy1.name = 'proxy'
    proxy1.greet('玖哲')
    console.log(obj1);
    console.log(proxy1);
</script>
<script>
    // 用于代理对象是函数时的代理。apply方法拦截函数的调用、call和apply操作。
    function getData(...args) {
        return args.join(',')
    }
    const proxy2 = new Proxy(getData, {
        apply(target, ctx, argArray) {
            return argArray.join('&&&&')
        }
    })
    console.log(proxy2('ddd', 'ddd1'));

</script>
</body>
</html>
